package com.ibiaoke.goods.service;

import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.goods.dao.ShopPriceDao;
import com.ibiaoke.goods.entity.ShopPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopPriceServiceImpl implements ShopPriceService {
    @Autowired
    private ShopPriceDao shopPriceDao;

    @Override
    public Integer batchSaveShopPrice(List<ShopPrice> shopPrices) {
        AssertUtils.notEmpty(shopPrices, "参数不正确，请检查参数");

        shopPrices.forEach(ShopPriceServiceImpl::validate);

        Integer goodsId = shopPrices.get(0).getGoodsId();

        shopPriceDao.delShopPrice(goodsId);
        int count=0;
        for(ShopPrice shopPrice:shopPrices){
            shopPrice.setShopPriceId(null);
            shopPriceDao.insertSelective(shopPrice);
            count++;
        }
        return count;
    }

    @Override
    public List<ShopPrice> listShopPrice(Integer goodsId) {
        AssertUtils.notNull(goodsId,"商品id不能为空");
        return shopPriceDao.listShopPrice(goodsId);
    }

    private static void  validate(ShopPrice price){
        AssertUtils.notNull(price.getShopId(),"店铺id不能为空");
        AssertUtils.notNull(price.getGoodsId(),"商品id不能为空");
        AssertUtils.notNull(price.getStockNum(),"库存不能为空");
        AssertUtils.notNull(price.getPrice(),"价格不能为空");
    }
}
